
package com.v1.video.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.v1.video.R;
import com.v1.video.engine.RequestEngine;
import com.v1.video.util.Utility;
import com.v1.video.util.Utils;

public class SettingForget1Activity extends BaseActivity implements OnClickListener {
    private EditText editText_phone;
//    private EditText editText_password; 
    private TextView txtTitle;
    private Button btnSure;
    private final int STATE_GET_VCODE_SUCCESS = 0X00001;
    private final int STATE_GET_VCODE_FAIL = 0X00002;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_login_forget);
    }

    @Override
    protected void initView() {
        editText_phone = (EditText)findViewById(R.id.editText_phone);
//        editText_password = (EditText)findViewById(R.id.editText_newpassword);
        txtTitle = (TextView)findViewById(R.id.tv_title);
        btnSure = (Button)findViewById(R.id.forget_btn_sure);
    }

    @Override
    protected void initData() {
    	txtTitle.setText(getResources().getString(R.string.setting_login_forget));
    	btnSure.setEnabled(false);
//    	((TextView)findViewById(R.id.tv_right)).setText("下一 步");
    }

    @Override
    protected void setListener() {
//        findViewById(R.id.tv_right).setVisibility(View.VISIBLE);
        findViewById(R.id.forget_btn_sure).setOnClickListener(this);
        findViewById(R.id.lay_result).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        
        editText_phone.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				int length = editText_phone.getText().toString().length();
				if(length <= 0){
					btnSure.setEnabled(false);
				}else{
					btnSure.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_btn_sure:
            case R.id.tv_right:
            	phoneNum = editText_phone.getText().toString();
//            	String password = editText_password.getText().toString();
            	if(phoneNum == null || phoneNum.equals("")){
    				Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
    				return;
    			}
//            	if(password == null || password.equals("")){
//            		ToastAlone.showToast(SettingForget1Activity.this, "请输入密码", Toast.LENGTH_SHORT);
//            		return;
//            	}
    			
//    			Pattern p2 = Pattern.compile("[a-zA-Z][_a-zA-Z0-9]");
            	Pattern p2 = Pattern.compile("^[A-Za-z0-9]+$");
    			
//            	Matcher m2 = p2.matcher(password);
//            	boolean b2 = m2.matches();
//            	if(password.length() < 6 || password.length() > 20|| !b2 ){
//            		Toast.makeText(this, "密码格式错误", Toast.LENGTH_SHORT).show();
//            		return;
//            	}
            	if(checkPhoneNum()){
    				if(!Utility.isConnected(SettingForget1Activity.this)){
    	    			Toast.makeText(this, "无网络连接，请检查网络设置", Toast.LENGTH_SHORT).show();
    					return;
    	    		}
    				progDialog = Utils.getProgressDialog(SettingForget1Activity.this,"请稍候");
//    				progDialog = ProgressDialog.show(this, null,"请稍候", true, false);
//    				getVcode(phoneNum);
    				RequestEngine request = new RequestEngine();
    				request.getVcode(SettingForget1Activity.this,phoneNum, "",mHandler,STATE_GET_VCODE_SUCCESS,STATE_GET_VCODE_FAIL);
    			}
    			else{
    				Toast.makeText(SettingForget1Activity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
    			}
            	
                break;
        }

    }
    
    private String phoneNum;
	private ProgressDialog progDialog;
	
	private boolean checkPhoneNum(){
		
		Pattern p = Pattern.compile("^((1[0-9][0-9])|(1[0-9][^4,\\D])|(1[0-9][0,5-9]))\\d{8}$");  
		Matcher m = p.matcher(editText_phone.getText().toString());
		return m.matches(); 
		
	}
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case STATE_GET_VCODE_SUCCESS:
				if (progDialog != null) {
					progDialog.dismiss();
				}
				progDialog = null;
				Intent intent = new Intent(
						SettingForget1Activity.this,
						SettingForget2Activity.class);

				intent.putExtra("phoneNum", phoneNum);
//				intent.putExtra("password",
//						editText_password.getText()
//								.toString());
				startActivity(intent);
				finish();
				break;
			case STATE_GET_VCODE_FAIL:
				if (progDialog != null) {
					progDialog.dismiss();
				}
				progDialog = null;
				break;
			default:
				break;
			}
		}		
	};
	
	/**
	 * 获取验证码线程
	 * 
	 * @param phoneNum
	 */
//	private void getVcode(final String phoneNum){
//		
//		new AsyncTask<Void, Void, BaseInfo>(){
//
//			@Override
//			protected BaseInfo doInBackground(Void... params) {
//				// TODO Auto-generated method stub
//				BaseInfo baseInfo = null;
//				try {
//					baseInfo = new NetEngine().getVcode(phoneNum,Constant.GET_VCODE_FORGET+"");
//				} catch (NetException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				progDialog.dismiss();
//				progDialog = null;
//				return baseInfo;
//				
//			}
//
//			@Override
//			protected void onPostExecute(BaseInfo result) {
//				// TODO Auto-generated method stub
//				super.onPostExecute(result);
//				
//				if(result != null){
//					
//					if(result.getCode().equals("1")){
//						Intent intent = new Intent(SettingForget1Activity.this,SettingForget2Activity.class);
//						
//						intent.putExtra("phoneNum", phoneNum);
////						intent.putExtra("password", editText_password.getText().toString());
//						startActivity(intent);
//						finish();
//					}
//					else{
//						Toast.makeText(SettingForget1Activity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
//					}
//					
//				}
//				else{
//					Toast.makeText(SettingForget1Activity.this, "获取验证码失败", Toast.LENGTH_SHORT).show();
//				}
//				
//			}
//			
//		}.execute();
//		
//	}
    
}
